package com.linwen.javase.desgin_pattern.structure.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @ClassName DynamicProxy
 * @Description
 * @Author lin
 * @Date 2022/4/13 下午10:34
 * @Version V1.0
 */
public class DynamicProxy {
    private Object object;

    DynamicProxy(Object o){
        this.object = o;
    }

    Object getProxyInstance(){
        return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                long start = System.currentTimeMillis();
                System.out.println("执行"+method.getName()+"方法前加一些日志");
                Object invoke = method.invoke(object, args);
                System.out.println("执行该方法共耗时"+(System.currentTimeMillis()-start)+"ms");
                return invoke;
            }
        });
    }
}
